/**
 * 
 */
package com.wordpress.sharebyblog.world.services;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.wordpress.sharebyblog.world.dao.CityDao;
import com.wordpress.sharebyblog.world.dao.CountryDao;
import com.wordpress.sharebyblog.world.dao.CountryLanguageDao;
import com.wordpress.sharebyblog.world.domain.City;
import com.wordpress.sharebyblog.world.domain.Country;
import com.wordpress.sharebyblog.world.exception.WorldServiceException;

/**
 * @author Bharat Mehta
 *
 */
public class WorldServiceImpl implements WorldService {
	
	@Autowired
	private CountryDao countryDao;
	@Autowired
	private CityDao    cityDao;
	@Autowired
	private CountryLanguageDao countryLanguageDao;
	
	private static final Logger logger = LoggerFactory.getLogger(WorldServiceImpl.class);

	@Override
	public List<String> findRegions() throws WorldServiceException  {
		List<String> regions = null;
		try{
			regions = countryDao.findRegions();
		}catch(Throwable e){
			logger.error("Problemns occured while fetching regions.", e);
			throw new WorldServiceException(e.getMessage(), e);
		}
		return regions;
	}

	@Override
	public List<Country> findCountriesByContinent(String continent) throws WorldServiceException {
		List<Country> countries = null;
		try{
			if(StringUtils.hasText(continent)){
				countries = countryDao.findCountriesByContinent(continent);
			}else{
				throw  new IllegalArgumentException("Invalid continent name.");
			}
			
		}catch(Throwable e){
			logger.error("Problems occurred while  finding countries by continent.",e);
			throw new WorldServiceException(e.getMessage(),e);
		}
		return countries;
	}

	@Override
	public List<City> findCitiesByCountry(String countryCode) throws WorldServiceException {
		List<City> cities = null;
		try{
			if(StringUtils.hasText(countryCode)){
				cities = cityDao.findCitiesByCountryCode(countryCode);
			}else{
				throw new IllegalArgumentException("Invalid country code.");
			}
			
		}catch(Throwable e){
			logger.error("Problems occured while finding cities by country.",e);
			throw new WorldServiceException(e.getMessage(), e);
		}
		return cities;
	}

	@Override
	public City findCapital(String countryCode) throws WorldServiceException {
		City capital = null;
	    try {
			Country country = countryDao.findByCountryCode(countryCode);
			if(country != null){
				capital = country.getCapital();
				
			}else{
				new IllegalArgumentException("Invalid CountryCode");
			}
		} catch (Exception e) {
			logger.error("Problems occured while finding capital for the countryCode,"+countryCode,e);
			throw new WorldServiceException(e.getMessage(), e);
		}
	    return capital;
	}

	@Override
	public Country findCountryByCode(String countryCode) throws WorldServiceException {
		Country country = null;
	    try {
	    	if(!StringUtils.hasText(countryCode)){
	    		throw new IllegalArgumentException("Invalid Country Code");
	    	}
			country = countryDao.findByCountryCode(countryCode);
			
		} catch (Exception e) {
			logger.error("Problems occured while finding Country for the countryCode,"+countryCode,e);
			throw new WorldServiceException(e.getMessage(), e);
		}
	    return country;
	}

	/**
	 * @return the countryDao
	 */
	public CountryDao getCountryDao() {
		return countryDao;
	}

	/**
	 * @param countryDao the countryDao to set
	 */
	public void setCountryDao(CountryDao countryDao) {
		this.countryDao = countryDao;
	}

	/**
	 * @return the cityDao
	 */
	public CityDao getCityDao() {
		return cityDao;
	}

	/**
	 * @param cityDao the cityDao to set
	 */
	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	/**
	 * @return the countryLanguageDao
	 */
	public CountryLanguageDao getCountryLanguageDao() {
		return countryLanguageDao;
	}

	/**
	 * @param countryLanguageDao the countryLanguageDao to set
	 */
	public void setCountryLanguageDao(CountryLanguageDao countryLanguageDao) {
		this.countryLanguageDao = countryLanguageDao;
	}
	
	
	
	

}
